package com.wind.taobao.core.util
{


	import com.adobe.crypto.MD5;

	import flash.net.URLRequest;
	import flash.net.navigateToURL;
	import flash.text.StyleSheet;

	import mx.collections.ArrayCollection;
	import mx.collections.ICollectionView;
	import mx.collections.Sort;
	import mx.collections.SortField;
	import mx.controls.DateField;
	import mx.formatters.DateFormatter;

	/**
	 * 淘宝辅助类
	 * @author fxy
	 */
	public class TaobaoUtils
	{

		/**
		 * 与服务器时间差
		 */
		[Bindable]
		public static var timeDifference:Number=60*10000*1000;

		/**
		 * 参数签名
		 * @param properties
		 * @param secret
		 * @param signName
		 * @return
		 *
		 */
		public static function signature(properties:ArrayCollection, secret:String, signName:String="sign"):String
		{
			if (properties.length > 0)
			{
				var sort:Sort=new Sort();
				sort.fields=[new SortField("key")];
				properties.sort=sort;
				properties.refresh();
				var orgin:String=secret;
				for (var i:int=0; i < properties.length; i++)
				{
					orgin+=properties[i].key + properties[i].value;

				}
			}
			try
			{
				return MD5.hash(orgin).toUpperCase();
			}
			catch (error:Error)
			{
				trace(error);
				throw new Error("sign error");
			}
			return null;
		}

		/**
		 * 将Date类型的时间转换为以"yyyy-MM-dd HH:mm:ss.SSS"为格式的字符串形式
		 *
		 * @param date
		 * @return
		 */
		public static function dateToStr(date:Date, formatString:String="YYYY-MM-DD HH:NN:SS"):String
		{
			var format:DateFormatter=new DateFormatter();
			format.formatString=formatString;
			return format.format(date);
		}

		public static function strToDate(str:String, format:String="YYYY-MM-DD HH:NN:SS"):Date
		{
			var date:Date=DateField.stringToDate(str, "YYYY-MM-DD");
			var hns:Array=String(str.split(" ")[1]).split(":");
			if (hns.length == 3)
				date.setHours(hns[0], hns[1], hns[2]);
			return date;
		}

		/**
		 * 获取数组
		 * @param value
		 * @return
		 *
		 */
		public static function getArray(value:Object):Array
		{
			if (value is Array)
			{
				return value as Array;
			}
			else
			{
				return [value];
			}
		}

		/**
		 * 字符串有值
		 * @param value
		 * @return
		 *
		 */
		public static function hasValue(value:String):Boolean
		{
			return value != null && value.length > 0 && value != "null";
		}

		/**
		 * 数组中至少有一个有值
		 * @param values
		 * @return
		 *
		 */
		public static function hasAnyValue(values:Array):Boolean
		{
			if (values != null && values.length > 0)
			{
				for (var i:uint=0; i < values.length; i++)
				{
					if (hasValue(String(values[i])))
					{
						return true;
					}
				}
			}
			return false;
		}

		/**
		 * 数组内元素均有值
		 * @param values
		 * @return
		 *
		 */
		public static function hasAllValue(values:Array):Boolean
		{
			if (values != null && values.length > 0)
			{
				for (var i:uint=0; i < values.length; i++)
				{
					if (values[i] is String)
					{
						if (!hasValue(String(values[i])))
						{
							return false;
						}
					}
					else
					{
						if (values[i] == null)
							return false;
					}
				}
			}
			else
			{
				return false;
			}
			return true;
		}

		/**
		 * 转换对象
		 * @param fromObj
		 * @param toObj
		 * @return
		 *
		 */
		public static function convertObject(fromObj:Object, toObj:Object):Object
		{
			if (fromObj != null && toObj != null)
			{
				for (var prop:String in fromObj)
				{
					if (toObj.hasOwnProperty(prop) && fromObj[prop])
					{
						toObj[prop]=fromObj[prop];
					}
				}
			}
			return toObj;
		}

		/**
		 * 打开URL
		 * @param url
		 * @param target
		 *
		 */
		public static function openUrl(url:String, target:String="_blank"):void
		{
			flash.net.navigateToURL(new URLRequest(url), target);
		}

		/**
		 * 获取URL参数
		 * @param url
		 * @param parameter
		 * @return
		 *
		 */
		public static function getUrlParameter(url:String, parameter:String):String
		{
			var start:int=url.indexOf(parameter + "=");
			if (start == -1)
			{
				return null;
			}
			var len:int=start + parameter.length + 1;
			var end:int=url.indexOf("&", len);
			if (end == -1)
			{
				end=url.length;
			}
			return url.substring(len, end);
		}

		/**
		 * 判断集合是否包含指定对象（指定比较域）
		 * @param arry
		 * @param item
		 * @param fields
		 * @return
		 *
		 */
		public static function containsItem(arry:ICollectionView, item:Object, fields:Array):Boolean
		{
			return getItemIndex(arry, item, fields) != -1 ? true : false;
		}

		/**
		 * 获取集合指定对象的位置
		 * @param arry
		 * @param item
		 * @param fields
		 * @return
		 *
		 */
		public static function getItemIndex(arry:ICollectionView, item:Object, fields:Array):int
		{
			if (arry && arry.length > 0 && item && fields && fields.length > 0)
			{
				for (var i:int=0; i < arry.length; i++)
				{
					var isContains:Boolean=true;
					for each (var field:String in fields)
					{
						if (arry[i][field] != item[field])
						{
							isContains=false;
						}
					}
					if (isContains)
					{
						return i;
					}
				}
			}
			return -1;
		}

		/**
		 * 为一个StyleSheet添加一个样式
		 * 样式的名称为styleName
		 * 样式内容放在sytle中
		 */
		public static function addStyle(styleName:String, style:Object, styleSheet:StyleSheet):void
		{
			styleSheet.setStyle(styleName, style);
		}
	}
}